package com.msb.c12;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;

/**
 * @author: xw
 * @create: 2022/10/13 14:46
 * @description:一个线程输出A-Z，另一个线程输出 1-26，交替输出
 */
public class Code_AlternatePrint {
    static Thread t1 = null, t2 = null;
    public static void main(String[] args) throws InterruptedException {
        AtomicReference<Character> d = new AtomicReference<>('A');
        String[] a = {"a","b","c"};
        Integer[] b = {1,2,3};
        final Object lock = new Object();
        t1 = new Thread(()->{
            synchronized (lock){
//                for (int i = 0; i < 26; i++) {
//                    System.out.println(d.getAndSet(new Character((char) (d.get() + 1))));
//
//                    try {
//                        lock.notify();
//                        lock.wait();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
                for (int i = 0; i < a.length; i++) {
                    System.out.println(a[i]);
                    try {
                        lock.notify();
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }

        },"t1");

        t2 = new Thread(()->{
//            synchronized (lock){
//                for (int i = 1; i <= 26; i++) {
//                    System.out.println(i);
//                    try {
//                        lock.notify();
//                        lock.wait();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
            synchronized (lock){
                for (int i = 0; i < b.length; i++) {
                    System.out.println(b[i]);
                    try {
                        lock.notify();
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        },"t2");

        t1.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();




    }
}
